﻿namespace MemoryTest_Communications.Model
{
    using System.Collections.Generic;
    using System.Linq;
    using WP7Contrib.Collections;
    using WP7Contrib.Common;

    public sealed class PropertyResult : BaseModel
    {
        private readonly ObservableCollection<Location> locations;

        private IPagedList<Property> properties;
        private string responseCode;
        private string responseText;
        private ResponseStatus responseStatus;

        public PropertyResult()
        {
            locations = new ObservableCollection<Location>();
            properties = new PagedList<Property>();
        }

        public string ResponseText
        {
            get { return responseText; }
            set { this.SetPropertyAndNotify(ref this.responseText, value, () => this.ResponseText); }
        }

        public string ResponseCode
        {
            get { return responseCode; }
            set
            {
                if (value != this.responseCode)
                {
                    if (value == "100" || value == "101" || value == "110" || value == "111")
                        this.responseStatus = ResponseStatus.Ok;
                    else if (value == "200" || value == "201" || value == "202" || value == "210")
                        this.responseStatus = ResponseStatus.BadLocation;
                    else if (value == "900" || value == "901" || value == "902")
                        this.responseStatus = ResponseStatus.InvalidRequest;
                    else if (value == "500")
                        this.responseStatus = ResponseStatus.InternalError;
                    else if (value == "910" || value == "911")
                        this.responseStatus = ResponseStatus.InvalidVersion;
                }
                this.SetPropertyAndNotify(ref this.responseCode, value, () => this.ResponseCode);
            }
        }

        public ResponseStatus ResponseStatus
        {
            get { return this.responseStatus; }
        }

        public ReadOnlyObservableCollection<Location> Locations
        {
            get { return new ReadOnlyObservableCollection<Location>(locations); }
        }

        public IPagedList<Property> Properties
        {
            get { return properties; }
            set { this.SetPropertyAndNotify(ref this.properties, value, () => this.Properties); }
        }

        public PropertyResult AddLocation(Location location)
        {
            this.locations.Add(location);

            return this;
        }

        public PropertyResult AddLocations(IEnumerable<Location> locationArray)
        {
            locationArray.ToList().ForEach(l => this.locations.Add(l));

            return this;
        }

        public bool IsLocationUnambiguous
        {
            get { return ResponseCode == "100"; }
        }

        public bool IsEmptyLocation
        {
            get { return this.Properties.Count() == 0; }
        }

        public bool IsLocatioBestGuessOfAmbiguous
        {
            get { return ResponseCode == "101"; }
        }

        public bool IsLocationLarge
        {
            get { return ResponseCode == "110"; }
        }

        public bool IsGuidSpecified
        {
            get { return ResponseCode == "111"; }
        }

        public bool IsLocationAmbiguous
        {
            get { return ResponseCode == "200";  }
        }

        public bool IsUnknownLocation
        {
            get { return ResponseCode == "201"; }
        }

        public bool IsMissSpelledLocation
        {
            get { return ResponseCode == "202"; }
        }

        public bool IsCoordinateError
        {
            get { return ResponseCode == "210"; }
        }
    }
}